
* EVENT STUDY INMOVERS

* * *   * * *   * * *   * * *   * * *   * * *
* PREPARE DATA FOR EVENT STUDY 
* * *   * * *   * * *   * * *   * * *   * * *

cd "$mypathRR/Results/"

local b " 55 60 60 60"
local t " 75 80 85 95"
local n : word count `b'

forvalues i = 1/`n' {
local x : word `i' of `b'
local y : word `i' of `t'
cap drop treat_i
gen treat_i=.
replace treat_i=0 if ctrl_ir_`x'_`y'==1
replace treat_i=1 if taxinc_thre2==1
label var treat_i "Income tax treatment"


* incomers only, INCOME CONTROL GROUP 
preserve

sort persid zuzugjahre year
keep if freq==1
*drop if year>2007
*drop if zuzugjahre>2007


collapse (count) Inmovers=persid (mean) avgtt=avgt_stek_taxable (mean) avgtg=avgt_stek_gross (mean) mtr=mtr_total , by(zuzugjahre treat_i)
label var Inmovers "Incomers"
label var avgtt "Average tax rate on taxable income"
label var avgtg "Average tax rate on gross income"
label var mtr "Marginal tax rate on taxable income"
label var treat_i "Treatment"
label var zuzugjahre "Year"

xtset treat_i zuzugjahre
tsfill, full
replace Inmovers=0 if Inmovers==.

list // there are observations in every, year; avgtg missing in 2004 for treated bc components of gross-income variable missing in some cases - this is now corrected

gen Period=(zuzugjahre>=2006)
gen DiD=treat_i*Period
gen weight=Inmovers

drop if DiD==.


* Gen interaction terms for pre-treatment periods
forval n=1/5 {
local i= 2006 -`n'
gen pre`n'=0
replace pre`n'= 1 if treat_i == 1 & zuzugjahre == `i'
label var pre`n' "`i' (treatment lag `n')"
}
replace pre1 = 0 // make 2005 the reference year


* Gen interaction terms for post-treatment periods
forval n=1/5 {
local i = 2005 + `n'
gen post`n' = 0
replace post`n' = 1 if treat_i == 1 & zuzugjahre == `i'
label var post`n' "`i' (treatment lead `n')"
}

* Gen group-specific time trends
	gen group_0 = treat_i == 0
	gen group_1 = treat_i == 1
	foreach var of varlist group_*  {
	gen trend_`var' = zuzugjahre*`var'
	label var trend_`var' "Group-specific time trend"
	}
	gen trend = zuzugjahre
	
gen ln_Inmovers = ln(Inmovers)



* * *   * * *   * * *   * * *   * * *   * * *
* EVENT STUDY GRAPHS
* * *   * * *   * * *   * * *   * * *   * * *


* make lists of coefficients
global coeflist " pre5 pre4 pre3 pre2 pre1 post1 post2 post3 post4 post5 " //2001-2010
*global coeflist " pre5 pre4 pre3 pre2 pre1 post1 post2 " //2001-2007

// change outcomes as needed
global outcomes "Inmovers ln_Inmovers"


foreach outcome in $outcomes {
	sum `outcome' [aweight = weight] if treat_i == 1 & zuzugjahre == 2005
	global `outcome'2005 = `r(max)'
	
reghdfe `outcome' $coeflist [aweight = weight], vce(robust) noabsorb
		est sto model_`outcome'_`x'_`y'
}					

	restore
}
	

	coefplot (model_Inmovers_55_75, label(" ") keep($coeflist ) omitted color(navy*0.55) ciopts(recast(rarea) color(navy*0.05) color(navy%10)))  ///
			 (model_Inmovers_60_95, label(" ") keep($coeflist ) omitted color(maroon*0.65) ciopts(recast(rarea) color(maroon*0.01) color(maroon%10)))  ///				
			 (model_Inmovers_60_85, label(" ") keep($coeflist ) omitted color(midblue*0.6) ciopts(recast(rarea) color(midblue*0.1) color(midblue%10)))  ///
			 (model_Inmovers_60_80, label(" ") keep($coeflist ) omitted color(midblue*1.1) ciopts(recast(rarea) color(midblue*0.2) color(midblue%20)))  ///
			 , ///
					order($coeflist ) ///
					coeflabels(pre5 = "2001" pre4 = "2002" pre3 = "2003" pre2 = "2004" pre1 = "2005" ///
					post1 = "2006" post2 = "2007" post3 = "2008" post4 = "2009" post5 = "2010") ///
					vertical levels(95) ciopts(recast(rarea)) connect(direct) nooffsets ///
					yline(0, lcolor(black))  xline(5.5, lcolor(red)) xline(7.5, lcolor(green)) ///
					legend(title("Control group (% of threshold)", size(small)) ///
						order(2 "55-75%" 8 "60-80%" 6 "60-85%" 4 "60-95%") row(2) ring(0) position(11) bmargin(vsmall) size(small)) ///
					xlabel(, labsize(small) ang(45)) ylabel(-30(5)30, labsize(small) grid) ///
					ytitle("Inmovers (treated - control)") name(gr_Inmovers_all, replace) 
	graph export "Fig_6b)-ES_Inmovers-all_ctrl_groups.pdf", as(pdf)	replace

